package org.sunrise.game.global.service.union;

import org.sunrise.game.global.gen.CallEnum;
import org.sunrise.game.rpc.annotation.RpcMethod;
import org.sunrise.game.rpc.annotation.RpcService;
import org.sunrise.game.rpc.function.Call;
import org.sunrise.game.rpc.function.CallContext;
import org.sunrise.game.rpc.function.RpcFunction;
import org.sunrise.game.rpc.service.BaseService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RpcService
public class UnionService extends BaseService {

    private Map<Integer, List<Long>> datas = new HashMap<>();

    public UnionService(String nodeId) {
        super(nodeId);
    }

    public void init() {
        super.init();
        // 假设加载出了数据
        List<Long> list = new ArrayList<>();
        list.add(100L);
        list.add(101L);
        datas.put(1, list);
    }

    @RpcMethod
    public void getData(int id) {
        Call lastCall = CallContext.getLastCall();
        RpcFunction rpcFunction = RpcFunction.newInstance();
        rpcFunction.call(CallEnum.PvpService_getData, "id", "PvpService");
        rpcFunction.listenResult(rpcResult -> {
            String r = (String) rpcResult.getData("data");
            System.out.println(r);
            returns(lastCall, "data", datas.get(id));
        });
    }

    @RpcMethod
    public void getData1() {
        returns("data1", datas);
    }

    @RpcMethod
    public void getData2() {
        List<Integer> list = new ArrayList<>();
        list.add(999);
        list.add(990);
        returns("data2", list);
    }
}

